<?php

namespace Admin\Controller;

use Common\Controller\AdminController;

class QuestionController extends AdminController {

    public function index() {
        $account_user_id = $_SESSION['account_user_id'];
        $questionModel = new \Admin\Model\QuestionModel();
        $where = array();
        $param = array();
        $result = $questionModel->getList($where, $param);
        $this->list = $result['list'];
        $this->page = $result['page'];
        if (IS_AJAX) {
            $this->ajaxReturn($this->fetch('Question/index_ajax'));
        } else {
            $this->display();
        }
    }

    public function addQuestion() {
        $account_user_id = $_SESSION['account_user_id'];
        $questionModel = new \Admin\Model\QuestionModel();
        if (!IS_POST) {
            $id = $_GET['id'];
            if (!empty($id) && isset($id)) {
                $question = $questionModel->getFInd(array('id' => $id));
                $this->find = $question;
            }

            $this->display();
        } else {
            $question = $_POST['question'];
            $ans = $_POST['ans'];
            $sort_num = $_POST['sort_num'];
            $inuse = $_POST['inuse'];
            $data['title'] = $question;
            $data['ans'] = $ans;
            $data['inuse'] = 2;
            $data['orderNum'] = $sort_num;
            $id = $_POST['id'];
            if (empty($id) && !isset($id)) {
                $data['createdt'] = date("Y-m-d H:i:s");
                $res = $questionModel->addData($data);
            } else {
                $where['id'] = $id;
                $res = $questionModel->saveData($where, $data);
            }
            if ($res) {
                $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => array()));
            } else {
                $this->ajaxReturn(array('code' => 10001, 'msg' => '请求失败', 'data' => array()));
            }
        }
    }

    public function saveInuse() {
        $id = $_POST['id'];
        $inuse = $_POST['inuse'];
        $questionModel = new \Admin\Model\QuestionModel();
        $where['id'] = $id;
        $data['inuse'] = $inuse;
        $result = $questionModel->saveData($where, $data);
        if ($result) {
            $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => array()));
        } else {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '请求失败', 'data' => array()));
        }
    }

}
